package kr.or.kead.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import kr.or.kead.domain.Depart;
import kr.or.kead.domain.Handicap;
import kr.or.kead.utils.MySqlCon;

public class DaoHandicap implements DaoTable {

	@Override
	public int insertDao(Object obj) {
		Connection con = MySqlCon.getConn();
		
		Handicap handi = (Handicap) obj;
		String sql = "insert into handicap values(?,?)";
		PreparedStatement pstmt=null;
		try {
			pstmt=con.prepareStatement(sql);
			pstmt.setInt(1, handi.getCode());
			pstmt.setString(2, handi.getName());
			pstmt.execute();
		} catch (SQLException e) {
			return -1;
		} finally {
			try {
				pstmt.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

	@Override
	public int updateDao(Object obj) {
Connection con = MySqlCon.getConn();
		
		Handicap handi = (Handicap) obj;
		String sql = "update handicap set "
				+ "code=?,"
				+ "name=?,"
				+ " where code=?";
		PreparedStatement pstmt=null;
		try {
			pstmt=con.prepareStatement(sql);
			pstmt.setInt(1, handi.getCode());
			pstmt.setString(2, handi.getName());
			pstmt.execute();
		} catch (SQLException e) {
			return -1;
		} finally {
			try {
				pstmt.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}
	@Override
	public int deleteDao(int regNo) {
		Connection con = MySqlCon.getConn();
		
		String sql = "delete from handicap where code=?";
		PreparedStatement pstmt=null;
		try {
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1,regNo);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
	@Override
	public ArrayList<Object> selectDao() {
		Connection con = MySqlCon.getConn();
		String sql = "select name from handicap";
		ArrayList<Object> handis = new ArrayList<>();
		PreparedStatement pstmt = null;
		ResultSet rs =null;
		try {
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()){
				handis.add(rs.getString(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			try { pstmt.close(); con.close(); } 
			catch (SQLException e) { e.printStackTrace(); }
		}
		return handis;
	}

	@Override
	public Object selectTableById(int stdId) {
		Connection con = MySqlCon.getConn();
		String sql = "select name from handicap where code=?";
		PreparedStatement pstmt = null;
		try {
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, stdId);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next())
				return rs.getString(1); 
			} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			try { pstmt.close(); con.close(); } 
			catch (SQLException e) { e.printStackTrace(); }
		}
		return null;
	}

	public int selectCodeHandicapByName(String name) {
		Connection con = MySqlCon.getConn();
		String sql = "select code from handicap where name=?";
		PreparedStatement pstmt = null;
		try {
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, name);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next())
				return rs.getInt(1);
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
		return -1;
	}

	@Override
	public ArrayList<String> selectTableAllList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object selectTableByEmail(String email) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updataTableByEmail(String pass, String email) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public ArrayList<String> selectTableAllNames() {
		// TODO Auto-generated method stub
		return null;
	}
	

}
